﻿using System;
using System.IO;
using System.Collections;
using System.Runtime.CompilerServices;
using UnityEngine;
using GJXQGame;

/// <summary>
/// 全局构造器，每个场景里都有，所以每个场景都会初始化一遍，也会初始化游戏管理器一次
/// 如果游戏管理器已经存在了，就跳过了，否则创建游戏管理器，来保证游戏里只有一个GameManager
/// </summary>
public class GameRoot : MonoBehaviour
{

    private GameManager _gameManager;
    public static GameRoot Instance;
    public GameObject m_GuiRoot = null;                                    //GUI 根目录对象
    public const string GUI_ROOT_NAME = "GUIRoot";                          //UIRoot对象名称
    public Camera m_UICamera = null;                                       //UI摄像机

    void Awake()
    {
        Instance = this;
        //是否在真机上
       
        InitGui(); //UI根节点初始化


        //采用更新模式
        //EventCore.addRMetaEventListener(GlobalManager.G_Event_FirstCopyServerFileFinish, InitCopyFinish);

        //LoadingWin.Instance.StartLoading(1, true, true, "解析中...");
        //SceneManager.Instance.Init();

        if (Application.platform == RuntimePlatform.WindowsPlayer
            || Application.platform == RuntimePlatform.OSXPlayer)
        {
            Screen.SetResolution(450, 800, false);
        }
    }

    void Start()
    {
        GameRoot.StartCoroutine(TableManager.GetInstance().LoadAll(() =>
        {

            //foreach (ChineseBase ChineseBase in TableManager.GetInstance().GetChineseTable().MapTable.Values)
            //{
            //    Debug.LogError(ChineseBase.TextID);
            //}

            //foreach (Act1Base Act1Base in TableManager.GetInstance().GetAct1Table().MapTable.Values)
            //{
            //    Debug.LogError(Act1Base.ID);
            //}
            //foreach (ArenaBaseBase ArenaBaseBase in TableManager.GetInstance().GetArenaBaseTable().MapTable.Values)
            //{
            //    Debug.LogError(ArenaBaseBase.id);
            //}
            //Debug.LogError("XXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
            //EnterMainMenu();
            this._gameManager = Singleton<GameManager>.GetInstance();
            this._gameManager.Initialize();
        }));
    }

    //第一次进入游戏时 拷贝资源完成 
   

    //初始化UI根节点
    public void InitGui()
    {
        m_GuiRoot = GameObject.Find(GUI_ROOT_NAME);
        if (m_GuiRoot != null) return;

        GameObject prefab = Resources.Load(GUI_ROOT_NAME) as GameObject;
        m_GuiRoot = Instantiate(prefab) as GameObject;
        m_GuiRoot.name = GUI_ROOT_NAME;
        m_UICamera = m_GuiRoot.GetComponentInChildren<Camera>();

        UIRoot root = m_GuiRoot.GetComponent<UIRoot>();
        root.scalingStyle = UIRoot.Scaling.Flexible;
        root.minimumHeight = 1280;
        root.maximumHeight = 1280;
    }

    private UICoroutine _coroutine;

    public static Coroutine StartCoroutine(IEnumerator routine)
    //{ return _instance.StartCoroutine_Auto(routine); }
    {
        if (Instance._coroutine == null)
        {
            Instance._coroutine = Instance.GetComponent<UICoroutine>();
        }
        return Instance._coroutine.Begin(routine);
    }

    public static IEnumerator StartCoroutine(IEnumerator routine, Action action)
    {
        yield return StartCoroutine(routine);
        //Coroutine coroutine = _instance.StartCoroutine_Auto(routine);
        //yield return coroutine;

        if (action != null) action();

        //return coroutine;
    }
}